<template>
  <div>
    <!-- <div class="toptitle">组织编码</div> -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="公司名称">
        <br />
        <br />
        <el-input v-model="formInline.companyName" placeholder @keyup.enter.native="search"
          style="margin-left: -60px; width: 200px"></el-input>
      </el-form-item>

      <el-form-item label="店铺编码">
        <br />
        <br />
        <el-input v-model="formInline.authCode" placeholder @keyup.enter.native="search"
          style="margin-left: -60px; width: 200px"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 950px; text-align: right">
        <el-button type="primary" @click="search()" size="mini" icon="el-icon-search">查询</el-button>
        <!-- </el-form-item>
            <el-form-item> -->
        <el-button type="primary" @click="kong()" size="mini" icon="el-icon-delete">清空</el-button>
        <!-- </el-form-item>
            <el-form-item> -->
        <el-button type="primary" @click="creatoneOrg()" size="mini" icon="el-icon-edit">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      style="width: 100%; margin-bottom: 20px" row-key="goodsTypeId" border v-loading="loading" default-expand-all>
      <el-table-column type="index" label="序号" align="center" width="60"></el-table-column>

      <el-table-column prop="companyName" label="公司名称" width="320"></el-table-column>
      <el-table-column prop="authCode" label="店铺编码" width="280"></el-table-column>
      <el-table-column prop="date" label="创建日期" width="280"></el-table-column>

      <el-table-column label="操作" width="180" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handledelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-form :inline="true" :model="formInline">
      <el-form-item style="margin-top: 6px"> </el-form-item>
    </el-form>
    <div class="block" style="margin-top: -49px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 50, 100]" :current-page="listQuery.current" :page-size="listQuery.size"
        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>

    <el-dialog title="信息" :visible.sync="dialogVisibleorg" width="50%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="ruleForm.companyName" maxlength="20"></el-input>
        </el-form-item>
        <el-form-item label="店铺编码" prop="authCode">
          <el-input v-model="ruleForm.authCode" maxlength="20"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleorg = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="confirmEditone()" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    queryGoodsTypeList,
    addGoodsType,
    addLowerGoodsType,
    deleteGoodsType,
    updaterole,
    getUserOrgType
  } from "@/api/headProductclass";
  import {
    getAllAuthCode,
    addAuhCode,
    deleteLoginAuthCode
  } from "@/api/codeManagement";
  import qs from "qs";
  export default {
    data() {
      return {
        action: `${process.env.VUE_APP_BASE_API}/api/file-service/v1/file/uploadFile`,
        headers: {
          token: localStorage.getItem("token"), //从cookie里获取token，并赋值  Authorization ，而不是token
        },
        tableData: [],
        txtVal: "",
        picUrl: "",
        loading: true,
        dialogVisibleorg: false,
        dialogVisibleorgone: false,
        imgListone: [],
        imgListtwo: [],
        ruleForm: {
          authCode: "",
          companyName: "",

        },
        orgSeqxia: "",
        ruleFormorg: {
          goodsTypeId: "",
          goodsTypeName: "",
          sortNo: 0,
          tagUrl: "",
          picUrl: "",
          isDisplay: 1,
          displayForm: 1,
          goodsTypeId: "",
          upGoodsType: ""
        },
        total: 0,
        options: [],
        optionsone: [],
        listQuery: {
          current: 1,
          size: 10
        },
        flagorg: false,
        dialogVisible: false,
        dialogVisibleImg: false,
        dialogImageUrl: "",
        formInline: {
          companyName: "",
          authCode: ""

        },
        rules: {
          companyName: [{
            required: true,
            message: "请输入公司名称",
            trigger: "blur"
          }],
          authCode: [{
            required: true,
            message: "请输入店铺编码",
            trigger: "blur"
          }],
          // tagUrl: [
          //     { required: true, message: "请上传图标", trigger: "blur" }
          // ],
          // picUrl: [
          //     { required: true, message: "请上传图片", trigger: "blur" }
          // ]
        }
      };
    },
    mounted() {
      this.rolemanageone();
    },
    methods: {
      creatoneOrg() {
        this.dialogVisibleorg = !this.dialogVisibleorg;
        this.ruleForm.goodsTypeName = "";
        this.ruleForm.sortNo = this.total + 1;
        (this.ruleForm.picUrl = ""), (this.ruleForm.isDisplay = 1);
        this.ruleForm.displayForm = 1;
        this.imgListtwo = [];
        this.imgListone = [];
      },
      creatoneOrgone(v) {
        console.log(v, "ll");
        this.flagorg = true;
        this.orgSeqxia = v.goodsTypeId;
        this.dialogVisibleorgone = !this.dialogVisibleorgone;
      },
      confirmEditone() {
        if (
          this.ruleForm.companyName == "" ||
          this.ruleForm.companyName == null
        ) {
          this.$message.error("请输入公司名称");
          return;
        }
        if (this.ruleForm.authCode == "" || this.ruleForm.authCode == null) {
          this.$message.error("请输入店铺编码");
          return;
        }
        let data = {
          authCode: this.ruleForm.authCode,
          companyName: this.ruleForm.companyName

        };
        addAuhCode(data).then(res => {
          if (res.code == 1) {
            this.$message({
              showClose: true,
              message: "添加成功",
              type: "success"
            });
            this.dialogVisibleorg = !this.dialogVisibleorg;
            this.rolemanageone();
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: "error"
            });
          }

        });
      },
      confirmEdittwo() {
        // if (
        //     this.ruleFormorg.tagUrl == "" ||
        //     this.ruleFormorg.tagUrl == null
        // ) {
        //     this.$message.error("请上传图标");
        //     return;
        // }
        // if (
        //     this.ruleFormorg.picUrl == "" ||
        //     this.ruleFormorg.picUrl == null
        // ) {
        //     this.$message.error("请上传图片");
        //     return;
        // }
        if (this.flagorg == false) {
          let dataone = {
            goodsTypeName: this.ruleFormorg.goodsTypeName,
            sortNo: this.ruleFormorg.sortNo,
            picUrl: this.ruleFormorg.picUrl,
            tagUrl: this.ruleFormorg.tagUrl,
            isDisplay: this.ruleFormorg.isDisplay,
            displayForm: this.ruleFormorg.displayForm,
            goodsTypeId: this.ruleFormorg.goodsTypeId
          };
          addGoodsType(dataone).then(res => {
            this.dialogVisibleorgone = !this.dialogVisibleorgone;
            this.$message({
              showClose: true,
              message: "修改成功",
              type: "success"
            });
            this.rolemanageone();
          });
        } else if (this.flagorg == true) {
          let data = {
            goodsTypeName: this.ruleFormorg.goodsTypeName,
            sortNo: this.ruleFormorg.sortNo,
            tagUrl: this.ruleFormorg.tagUrl,
            picUrl: this.ruleFormorg.picUrl,
            isDisplay: this.ruleFormorg.isDisplay,
            displayForm: this.ruleFormorg.displayForm,
            goodsTypeId: this.ruleFormorg.goodsTypeId,
            upGoodsType: this.orgSeqxia
          };
          addGoodsType(data).then(res => {
            this.$message({
              showClose: true,
              message: "添加成功",
              type: "success"
            });
            this.dialogVisibleorgone = !this.dialogVisibleorgone;
            this.rolemanageone();
          });
        }
      },
      updateStatus(goodsTypeId, status) {
        let data = {
          goodsTypeId: goodsTypeId,
          status: status
        };
        addGoodsType(data).then(res => {
          var msg = "";
          if (status == "1") {
            msg = "启用";
          } else if (status == "0") {
            msg = "停用";
          } else if (status == "2") {
            msg = "删除";
          }
          if (res.code == "1") {
            this.$message({
              showClose: true,
              message: msg + "成功",
              type: "success"
            });
            this.rolemanageone();
          } else {
            this.$message.error(msg + "失败");
          }
        });
      },
      changeorg(v) {
        this.dialogVisibleorgone = !this.dialogVisibleorgone;
        var userId = localStorage.getItem("userId");
        var picture = v.picUrl;
        this.imgListtwo = [];
        this.imgListone = [];
        if (v.tagUrl != "" && v.tagUrl != null) {
          let data = {
            name: "",
            url: v.tagUrl
          };
          this.imgListone.push(data);
        }
        if (picture != "" && picture != null) {
          let data = {
            name: "",
            url: picture
          };
          this.imgListtwo.push(data);
        }
        this.ruleFormorg.goodsTypeName = v.goodsTypeName;
        this.ruleFormorg.goodsTypeId = v.goodsTypeId;
        (this.ruleFormorg.sortNo = v.sortNo),
        (this.ruleFormorg.tagUrl = v.tagUrl),
        (this.ruleFormorg.picUrl = v.picUrl),
        (this.ruleFormorg.isDisplay = v.isDisplay),
        (this.ruleFormorg.displayForm = v.displayForm),
        (this.ruleFormorg.upGoodsType = v.upGoodsType),
        (this.flagorg = false);
      },
      kong() {
        this.formInline.companyName = "";
        this.formInline.authCode = "";

      },
      search() {
        this.listQuery.current = 1;
        let data = {
          size: this.listQuery.size,
          current: this.listQuery.current,
          condition: {
            companyName: this.formInline.companyName,
            authCode: this.formInline.authCode,

          }
        };
        getAllAuthCode(data).then(res => {
          this.total = res.total;
          this.tableData = res.records;
        });
      },
      indexMethod(index) {
        let curpage = this.listQuery.current; //单前页码，具体看组件取值
        let limitpage = this.listQuery.size; //每页条数，具体是组件取值
        return index + 1 + (curpage - 1) * limitpage;
      },
      handledelete(v) {
        let data = {
          uuid: v.uuid
        };
        this.$confirm("确认删除吗？").then(_ => {
          deleteLoginAuthCode(qs.stringify(data)).then(res => {
            this.$message({
              showClose: true,
              message: res.message,
              type: res.code == 1 ? "success" : "error"
            });
            this.rolemanageone();
          });
        });
      },
      add() {
        // this.dialogVisible = !this.dialogVisible;
        this.editflag = true;
      },
      edit(v) {
        console.log(v, "kkdd");
        this.ruleForm.orgName = v.orgName;
        this.ruleForm.goodsTypeId = v.goodsTypeId;
        this.ruleForm.upOrgSeq = v.upOrgSeq;
        this.ruleForm.upOrgSeqName = v.upOrgSeqName;
        this.ruleForm.orgType = v.orgType;
        this.ruleForm.updateUser = v.updateUser;
        this.ruleForm.sortNo = v.sortNo;
        this.ruleForm.tagUrl = v.tagUrl;
        this.ruleForm.picUrl = v.picUrl;
        console.log(v, "pp");
        // this.dialogVisible = !this.dialogVisible;
        this.editflag = false;
      },
      confirmEdit(v) {
        if (this.ruleForm.orgName == "" || this.ruleForm.orgName == null) {
          this.$message.error("请输入本级组织名称");
          return;
        }
        if (
          this.ruleForm.goodsTypeId == "" ||
          this.ruleForm.goodsTypeId == null
        ) {
          this.$message.error("请输入本级组织编码");
          return;
        }
        if (
          this.ruleForm.upOrgSeq == "" ||
          this.ruleForm.upOrgSeq == null
        ) {
          this.$message.error("请选择上级组织名称");
          return;
        }
        if (this.ruleForm.orgType == "" || this.ruleForm.orgType == null) {
          this.$message.error("请选择组织类型");
          return;
        }
        if (this.editflag) {
          //   var u1 = "u1";
          var userId = localStorage.getItem("userId");
          let data = {
            orgName: this.ruleForm.orgName,
            goodsTypeId: this.ruleForm.goodsTypeId,
            upOrgSeq: this.ruleForm.upOrgSeq,
            upOrgSeqName: this.ruleForm.upOrgSeqName,
            sortNo: this.ruleForm.sortNo,
            picUrl: this.ruleForm.picUrl,
            tagUrl: this.ruleForm.tagUrl,
            updateUser: this.ruleForm.updateUser
          };
          addGoodsType(data).then(res => {
            this.dialogVisible = !this.dialogVisible;
            this.$message({
              showClose: true,
              message: "添加成功",
              type: "success"
            });
            this.rolemanageone();
          });
        } else {
          //var u1 = "u1";
          var userId = localStorage.getItem("userId");
          let data = {
            orgName: this.ruleForm.orgName,
            goodsTypeId: this.ruleForm.goodsTypeId,
            picUrl: this.ruleForm.picUrl,
            tagUrl: this.ruleForm.tagUrl,
            updateUser: this.ruleForm.updateUser
          };
          updaterole(data).then(res => {
            this.dialogVisible = !this.dialogVisible;
            this.$message({
              showClose: true,
              message: res.message,
              type: "success"
            });
            this.rolemanageone();
          });
        }
      },
      handleSizeChange(val) {
        this.listQuery.size = val;
        this.rolemanageone();
      },
      handleCurrentChange(val) {
        this.listQuery.current = val;
        this.rolemanageone();
      },

      rolemanageone() {
        let data = {
          size: this.listQuery.size,
          current: this.listQuery.current,
          condition: {
            goodsTypeName: this.formInline.user,
            status: this.formInline.status
          }
        };
        getAllAuthCode(data).then(res => {
          res.records.forEach((item, i) => {
            this.options.push({
              value: item.goodsTypeId,
              label: item.orgName
            });
          });

          this.loading = false;
          this.total = res.total;
          this.tableData = res.records;
          console.log(this.tableData, "ppe");
        });

        //   let dataone = {
        //     orgType: "",
        //     orgTypeName: ""
        //   };

        //   getUserOrgType(dataone).then(res => {
        //     res.records.forEach((item, i) => {
        //       this.optionsone.push({
        //         value: item.orgType,
        //         label: item.orgTypeName
        //       });
        //     });
        //   });
      },
      descInput() {
        this.txtVal = this.updateDiseaseForm.memo.length;
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisibleImg = true;
      },
      upLoadPicone(file, fileList) {
        this.imgListone = [];
        this.ruleForm.tagUrl = file.result;
        this.ruleFormorg.tagUrl = file.result;
      },
      handleImgChangeone(file, fileList) {
        if (file.response) {
          this.imgListone.push({
            url: file.response.result,
            status: "finished"
          });
        }
      },
      upLoadPictwo(file, fileList) {
        this.imgListtwo = [];
        this.ruleForm.picUrl = file.result;
        this.ruleFormorg.picUrl = file.result;
      },
      handleImgChangetwo(file, fileList) {
        if (file.response) {
          this.imgListtwo.push({
            url: file.response.result,
            status: "finished"
          });
        }
      },
      checkSortNo(val, type) {
        // 值，符号
        if (val === null || val === undefined || val === "") {
          return this.total + 1;
        } else {
          var aaa = Number(this.total + 1);
          if (Number(val) < aaa) {
            this.$confirm(
                "此操作将排序小于推荐值" + aaa + ", 是否继续?",
                "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
                }
              )
              .then(() => {
                return (
                  (this.ruleForm.sortNo = val),
                  (this.ruleFormorg.sortNo = val)
                );
              })
              .catch(() => {
                return (
                  (this.ruleForm.sortNo = aaa),
                  (this.ruleFormorg.sortNo = aaa)
                );
              });
          } else {
            return val;
          }
        }
      },
      handleExceed(file, fileList6) {
        this.$message.error(`图片最多上传1张`)
      }
    }
  };
</script>
<style lang="less" scoped>
  .toptitle {
    border: 1px solid #e0e0e0;
    border-bottom: 2px solid #188097;
    background: #f5f5f5;
    line-height: 35px;
    margin-bottom: 10px;
    padding: 0 10px;
    font-size: 14px;
    font-weight: bold;
  }

  .el-form-item .el-select {
    width: 100%;
  }
</style>
